﻿using System;
using System.Threading;
using System.Windows.Forms;

namespace ComputerVision
{
    public partial class frmSplash : Form
    {
        // Threading.
        private const int TIMER_INTERVAL = 50;
        private static frmSplash _fSplash;
        private static Thread _oThread;

        // Fade in and out.
        private const double OpacityDecrement = .08;
        private double _opacityIncrement = .05;

        public frmSplash()
        {
            InitializeComponent();
            InitializeComponent();
            Opacity = .00;
            tmrTick.Interval = TIMER_INTERVAL;
            tmrTick.Start();
        }

        // Static methods.
        public static void ShowSplashScreen()
        {
            // Make sure it's only launched once.
            if (_fSplash != null)
            {
                return;
            }
            _oThread = new Thread(ShowForm) {IsBackground = true};
            _oThread.SetApartmentState(ApartmentState.STA);
            _oThread.Start();
            _oThread.Join();
        }

        // A private entry point for the thread.
        private static void ShowForm()
        {
            _fSplash = new frmSplash();
            Application.Run(_fSplash);
        }

        private void tmrTick_Tick(object sender, EventArgs e)
        {
            Opacity += _opacityIncrement;
            if (Opacity >= 1)
            {
                _opacityIncrement = -OpacityDecrement;
            }
            if (Opacity <= 0)
            {
                tmrTick.Stop();
                Close();
            }
        }

        private void frmSplash_Load(object sender, EventArgs e)
        {
            Size = BackgroundImage.Size;
        }
    }
}